package com.source.lightfowl.basedata.dao;

import com.source.lightfowl.basedata.entity.BarcodeMachine;
import com.source.lightfowl.basedata.entity.Stalls;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: cxy
 * @Description: 屠宰档口信息持久层接口
 */
public interface StallsDao extends JpaRepository<Stalls,Integer>, JpaSpecificationExecutor<Stalls> {

    @Query("from Stalls s where s.state = (?1) and s.stallsName LIKE ('%'||?2||'%')")
    List<Stalls> getLikeName(Integer state, String name);

    @Query("select id from Stalls s where s.stallsName like ('%'||?1||'%') or s.contacts like ('%'||?1||'%')")
    List<Integer> findIdByUserNameOrStallName(String name);

    @Query("select id from Stalls s where s.contacts like ('%'||?1||'%')")
    List<Integer> findIdByUserName(String name);


    @Query("select id from Stalls s where s.stallsName like ('%'||?1||'%')")
    List<Integer> findIdByStallName(String name);

    @Query("select id from Stalls s where s.state = (?1) and s.slaughterHouseId = (?2)")
    List<String> findBySlaughterHouseId(Integer state, String slaughterHouseId);

    @Query("select id from Stalls s where s.slaughterHouseId = (?1)")
    List<String> findBySlaughterHouseId(List<String> slaughterHouseId);

    List<Stalls> findByState(int state);


    @Modifying
    @Transactional
    @Query(nativeQuery = true,
            value = "update stalls set state = (?1) where id in (?2)")
    void changeBatch(Integer state, ArrayList<Integer> ids);

    Stalls findByIdAndStateNot(Integer id, int state);

    Page<Stalls> findByState(int state, Pageable pageable);

    @Modifying
    @Transactional
    @Query(nativeQuery = true,
    value = "update stalls set state = 0 where slaughterHouseId in (?1)")
    void deleteBySlaughterHouseId(ArrayList<String> slist);

    @Query(nativeQuery = true,
    value = "select id from stalls where userId = (?1)")
    List<String> findByUserId(String userId);

    //根据id查询所有数据
    @Query(nativeQuery = true,
            value = "select * from stalls where id = ?")
    Stalls findInfoById(String id);

    //根据id查询username
    @Query(nativeQuery = true,
    value = "select name from sys_user where id = ?1")
    String findUserNameById(String userId);

    @Query(nativeQuery = true,
    value = "select * from stalls where stallsName = ?")
    Stalls findInfoByName(String stallsName);

}
